package com.neusoft.handoverserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.handoverserver.dto.HandoverSearch;
import com.neusoft.handoverserver.dto.RespBean;
import com.neusoft.handoverserver.entity.CustomerHandover;
import com.neusoft.handoverserver.mapper.CustomerHandoverMapper;
import com.neusoft.handoverserver.service.ICustomerHandoverService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 客户交接表 服务实现类
 * </p>
 *
 * @author liuxp
 * @since 2021-07-12
 */
@Service
public class CustomerHandoverServiceImpl extends ServiceImpl<CustomerHandoverMapper, CustomerHandover> implements ICustomerHandoverService {
    @Autowired CustomerHandoverMapper customerHandoverMapper;
    public RespBean addCustomerHandover(CustomerHandover customerHandover){
        customerHandoverMapper.insert(customerHandover);
        RespBean respBean = RespBean.ok(200, "Handover added successfully!");
        return respBean;
    }

    @Override
    public RespBean searchHandover(HandoverSearch handoverSearch) {
        RespBean respBean = RespBean.ok(200, "Handover find successfully!");
        LambdaQueryWrapper<CustomerHandover> wrapper = Wrappers.lambdaQuery();
        if(hasValue(handoverSearch.getDeptReceive())){
            wrapper.eq(CustomerHandover::getDeptReceive, handoverSearch.getDeptReceive());
        }
        if(hasValue(handoverSearch.getDeptRemove())){
            wrapper.eq(CustomerHandover::getDeptRemove, handoverSearch.getDeptRemove());
        }
        if(hasValue(handoverSearch.getMgrReceive())){
            wrapper.eq(CustomerHandover::getMgrReceive, handoverSearch.getMgrReceive());
        }
        if(hasValue(handoverSearch.getMgrRemove())){
            wrapper.eq(CustomerHandover::getMgrRemove, handoverSearch.getMgrRemove());
        }
        if(hasValue(handoverSearch.getChanceName())){
            wrapper.eq(CustomerHandover::getChanceName, handoverSearch.getChanceName());
        }
        if(hasValue(handoverSearch.getCustomerName())){
            wrapper.eq(CustomerHandover::getCustomerName, handoverSearch.getCustomerName());
        }
        List<CustomerHandover> list = customerHandoverMapper.selectList(wrapper);
        respBean.setObj(list);
        return respBean;
    }

    private Boolean hasValue(String s){
        return s != null && s.length() > 0;
    }
}
